package com.loren.oauth.controller;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
public class OAuthClientController {

    @GetMapping({"", "/"})
    public Object getAccessToken(@RequestParam("code") String code) {
        RestTemplate restTemplate = new RestTemplate();
        String url = "http://127.0.0.1:9600/oauth/token?grant_type=authorization_code&client_id=test-resource-client-01&client_secret=123456&redirect_uri=http://127.0.0.1:9610&scope=all&code=";
        return restTemplate.getForObject(url + code, Object.class);
    }

    @GetMapping("/test-01")
    public String test01() {
        return "test01";
    }

    @GetMapping("/test-02")
    @PreAuthorize("hasAuthority('insert')")
    public String test02() {
        return "test02, insert";
    }

    @GetMapping("/test-03")
    @PreAuthorize("hasAuthority('update')")
    public String test03() {
        return "test03, update";
    }

    @GetMapping("/test-04")
    @PreAuthorize("hasAuthority('delete')")
    public String test04() {
        return "test04, delete";
    }

}
